package com.shujia.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo5MapReduceReadHbase {


    public static class ReadhbaseMapper extends TableMapper<Text, IntWritable> {
        /**
         * @param key     hbase中的rowkey
         * @param value   : hbase中的一个rowkey对应的数据
         * @param context ： 上下文对象
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
            String id = Bytes.toString(key.get());


            //取出班级
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));


            //输出到reduce端
            context.write(new Text(clazz), new IntWritable(1));

        }
    }

    public static class ReadHbaseReduce extends Reducer<Text, IntWritable, Text, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {


            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }

            String clazz = key.toString();

            String line = clazz + '\t' + sum;

            context.write(new Text(line), NullWritable.get());

        }
    }


    public static void main(String[] args) throws Exception {


        Configuration configuration = new Configuration();

        //指定zk的连接地址
        configuration.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        Job job = Job.getInstance(configuration);

        job.setJobName("readhbase");
        job.setJarByClass(Demo5MapReduceReadHbase.class);

        job.setNumReduceTasks(2);


        Scan scan = new Scan();


        //初始化map端，指定hbase表名
        TableMapReduceUtil.initTableMapperJob(
                "student",
                scan,
                ReadhbaseMapper.class,
                Text.class,
                IntWritable.class,
                job);


        //指定reduce
        job.setReducerClass(ReadHbaseReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);


        //指定输出路径
        FileOutputFormat.setOutputPath(job, new Path("/data/hbase/clazz_num"));


        //启动job
        job.waitForCompletion(true);

        /**
         *
         * 提交任务的时候使用带依赖的包
         *
         * hadoop jar  hbase-1.0-jar-with-dependencies.jar com.shujia.hbase.Demo5MapReduceReadHbase
         *
         */

    }
}
